﻿Imports System.Data.OleDb

Public Class frmNhapDiem_HocSinh
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Dim Diem As DTODiem = New DTODiem
    Dim IsAdd As Boolean = True
    Public Sub New(ByVal _diem As DTODiem)

        ' This call is required by the designer.
        InitializeComponent()
        Me.Diem = _diem
        IsAdd = False
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub frmNhapDiem_HocSinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strQuery As String = "Select * from MonHoc"
        Dim ds As DataSet = New DataSet()
        Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\database\database.mdb")
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(ds, "MonHoc")
        cbbMonHoc.DataSource = ds.Tables("MonHoc")
        cbbMonHoc.DisplayMember = "TenMonHoc"
        cbbMonHoc.ValueMember = "MaMonHoc"

        Dim list As List(Of Long) = New List(Of Long)
        For index = DateTime.Now.Year - 10 To DateTime.Now.Year
            list.Add(index)
        Next
        cbbNamHoc.DataSource = list

        If IsAdd = False Then
            txtMaHS.Text = Diem.MaHS
            txtMaHS.Enabled = False

            For index = 0 To ds.Tables("MonHoc").Rows.Count - 1

                If ds.Tables("MonHoc").Rows(index)("MaMonHoc") = Diem.MaMonHoc Then
                    cbbMonHoc.SelectedIndex = index
                    Exit For
                End If

            Next

            cbbMonHoc.Enabled = False
            nudHocKy.Value = Diem.HocKy
            nudHocKy.Enabled = False
            For index = 0 To list.Count - 1

                If list.Item(index) = Diem.NamHoc Then
                    cbbNamHoc.SelectedIndex = index
                    Exit For
                End If

            Next
            cbbNamHoc.Enabled = False
            txtDiem15Phut.Text = Diem.Diem15Phut
            txtDiem1Tiet.Text = Diem.Diem1Tiet
            txtDiemCuoiKy.Text = Diem.DiemCuoiKy
        End If

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim diem As DTODiem = New DTODiem
        diem.MaHS = txtMaHS.Text
        diem.MaMonHoc = cbbMonHoc.SelectedValue
        diem.HocKy = nudHocKy.Value
        diem.NamHoc = cbbNamHoc.SelectedValue
        diem.Diem15Phut = Convert.ToDouble(txtDiem15Phut.Text)
        diem.Diem1Tiet = Convert.ToDouble(txtDiem1Tiet.Text)
        diem.DiemCuoiKy = Convert.ToDouble(txtDiemCuoiKy.Text)
        Dim da As DAODiem = New DAODiem

        If IsAdd = True Then
            If da.Insert(diem, Application.StartupPath + "\database\database.mdb") Then
                Dim frm As frmBangDiem = New frmBangDiem
                frm.Show()
                Me.Close()
            Else
                MessageBox.Show("Nhập không thàng công.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Return
            End If
        Else

            If da.Update(diem, Application.StartupPath + "\database\database.mdb") Then
                Dim frm As frmBangDiem = New frmBangDiem
                frm.Show()
                Me.Close()
            Else
                MessageBox.Show("Cập nhật không thàng công.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                Return
            End If

        End If

    End Sub

End Class